package cz.mff.curreco.utils;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import cz.mff.curreco.model.MoneyItem;
import cz.mff.curreco.model.SessionObject;



public class MySaxHandler extends DefaultHandler {

    public MySaxHandler(SessionObject so) 
    {   
		this.so = so;
	}

    SessionObject so;
	ArrayList<MoneyItem> result = new ArrayList<MoneyItem>();
    Locator locator;
    //Acumulator for text reading    
    StringBuffer accumulator = new StringBuffer();
    MoneyItem actItem;  
    
    
   
    public void setDocumentLocator(Locator locator) 
    {
        this.locator = locator;
    }

    
    public void startDocument() throws SAXException 
    {
    	
       
    }
    
    public void endDocument() throws SAXException 
    {
    	so.setDescriptors(result);
    }    
    
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException 
    {
    	//Opening elements
        if(localName.equals("coin"))
        {
        	actItem = new MoneyItem(atts.getValue(4), Integer.parseInt(atts.getValue(0)), atts.getValue(2), atts.getValue(1), null, null);        	      	
        }
        if(localName.equals("circularDescriptor")|| localName.equals("pictureFrontSource")|| localName.equals("pictureBackSource") || localName.equals("fatCircularDescriptor"))
        {
        	accumulator.setLength(0);        	
        }
        if(localName.equals("pictureFrontSource"))
        {
        	actItem.setPictureFrontPath(atts.getValue(0));        	
        }
        if(localName.equals("pictureBackSource"))
        {
        	actItem.setPictureBackPath(atts.getValue(0));        	
        }
    }
    
    public void endElement(String uri, String localName, String qName) throws SAXException 
    {   	
    	
    	if(localName.equals("circularDescriptor"))
    	{
    		actItem.setCircularDescriptor(accumulator.toString().trim());
    	}
    	else if(localName.equals("coin"))
    	{
    		result.add(actItem);    		
    	}
    	else if(localName.equals("fatCircularDescriptor"))
    	{
    		actItem.setFatCircularDescriptor(accumulator.toString().trim());
    	}

    }
    
    
    public void characters(char[] ch, int start, int length) throws SAXException {
    	//Text reader
    	accumulator.append(ch, start, length);        
    }
    
    
    public void startPrefixMapping(String prefix, String uri) throws SAXException {
        
    }

    
    public void endPrefixMapping(String prefix) throws SAXException {
    
    }

    
    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
        
    }

   
    public void processingInstruction(String target, String data) throws SAXException {
         
    }

      
    public void skippedEntity(String name) throws SAXException {
    
    }
}